<template>
  <div>
    <!-- 弹框表单 -->
    <el-dialog
      :title="isAdd ? '添加菜单' : '编辑菜单'"
      center
      :visible.sync="isShow"
      :before-close="cancel"
    >
      <el-form ref="menu" :model="menuInfo" :rules="rules">
        <el-form-item
          prop="title"
          label="菜单名称"
          :label-width="formLabelWidth"
        >
          <el-input v-model="menuInfo.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="pid" label="上级菜单" :label-width="formLabelWidth">
          <!-- 上级菜单是一个下拉框 -->
          <el-select @change="changePid " v-model="menuInfo.pid" placeholder="请选择上级菜单">
            <el-option label="上级菜单" :value="0"></el-option>
            <el-option
              v-for="item in menulist"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="菜单类型" :label-width="formLabelWidth">
          <el-radio disabled v-model="menuInfo.type" :label="1">目录</el-radio>
          <el-radio disabled v-model="menuInfo.type" :label="2">菜单</el-radio>
        </el-form-item>
        <!-- 
          当选择目录的时候只能出现菜单图标  否则就要出现菜单地址
         -->
        <el-form-item
          v-if="menuInfo.type == 1"
          label="菜单图标"
          :label-width="formLabelWidth"
        >
          <el-select v-model="menuInfo.icon" placeholder="请选择">
            <el-option
              v-for="item in iconlist"
              :key="item"
              :label="item"
              :value="item"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-else label="菜单地址" :label-width="formLabelWidth">
          <el-select v-model="menuInfo.url" placeholder="请选择">
            <el-option
              v-for="item in indexRoutes"
              :key="item.path"
              :label="item.meta.title"  
              :value="item.path"
            ><!-- :label="item.path"也可以 -->
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="menuInfo.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button v-if="isAdd" type="primary" @click="addInfo"
          >添 加</el-button
        >
        <el-button v-else type="primary" @click="editInfo">编 辑</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
//引入封装好的菜单接口
import { menuAdd, menuInfo, menuEdit } from "../../request/api";
//引用辅助性函数
import { mapActions, mapGetters } from "vuex";
//引入封装好的  路由地址信息
import { indexRoutes } from "../../router";
export default {
  data() {
    return {
      //初始值
      menuInfo: {
        pid: 0,
        title: "",
        icon: "",
        type: 1,
        url: "",
        status: 1,
      },
      //验证规则
      rules: {
        title: [
          { required: true, message: "请输入菜单名称", trigger: "blur" },
          { min: 2, max: 8, message: "长度在 2 到 8 个字符", trigger: "blur" },
        ],
        pid: [{ required: true, message: "请选择上级菜单", trigger: "change" }],
      },
      formLabelWidth: "120px",
      //创建一个图标数组
      iconlist: [
        "el-icon-s-tools",
        "el-icon-setting",
        "el-icon-s-goods",
        "el-icon-goods",
        "el-icon-shopping-cart-full",
        "el-icon-shopping-cart-2",
        "el-icon-shopping-bag-1",
        "el-icon-shopping-bag-2",
        "el-icon-location",
        "el-icon-location-outline",
        "el-icon-folder-opened",
      ],
      indexRoutes: indexRoutes,
    };
  },
  computed: {
    ...mapGetters({
      menulist: "menu/getMenu",
    }),
  },
  props: ["isShow", "isAdd"],
  methods: {
    //封装一个change事件用来实现目录和菜单以及选择菜单的联动
    changePid(){
      this.menuInfo.type = this.menuInfo.pid==0?1:2
    },
    ...mapActions({ getMenu: "menu/getMenuAction" }),
    //封装一个关闭弹框事件
    cancel() {
      this.$emit("cancel", false);
      this.reset();
    },
    //封装一个清空事件
    reset() {
      this.menuInfo = {
        pid: 0,
        title: "",
        icon: "",
        type: 1,
        url: "",
        status: 1,
      };
      //清空表单验证
      this.$refs.menu.resetFields();
    },
    //封装一个菜单添加
    addInfo() {
      this.$refs.menu.validate((val) => {
        if (val) {
          // console.log(this.menuInfo,'输入表单');
          //调用菜单添加接口
          menuAdd(this.menuInfo).then((res) => {
            // console.log(res,'菜单添加');
            if (res.code == 200) {
              this.$message.success(res.msg);
              //关闭弹框
              this.cancel();
              //重新调用函数列表接口
              this.getMenu();
            } else {
              this.$message.error(res.msg);
            }
          });
        } else {
          console.log("验证失败");
        }
      });
    },
    //查询一条数据方法
    lookup(id) {
      //console.log(id,'列表的id');
      menuInfo({ id }).then((res) => {
        //console.log(res,'查询一条数据')
        if (res.code == 200) {
          this.menuInfo = res.list;
          //给编辑表单对象添加id
          this.menuInfo.id = id;
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    //封装一个菜单编辑
    editInfo() {
      this.$refs.menu.validate((val) => {
        if (val) {
          // console.log(this.menuInfo,'输入表单');
          //调用菜单编辑接口
          menuEdit(this.menuInfo).then((res) => {
            // console.log(res,'菜单添加');
            if (res.code == 200) {
              this.$message.success(res.msg);
              //关闭弹框
              this.cancel();
              //重新调用函数列表接口
              this.getMenu();
            } else {
              this.$message.error(res.msg);
            }
          });
        } else {
          console.log("验证失败");
        }
      });
    },
  },
};
</script>

<style scoped>
</style>